import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;

import java.util.ArrayList;

public abstract class Hasher {
  
  public static void main(String[] args) throws Exception {
    String fname = args[0];
    int numMachines = Integer.parseInt(args[1]);
    int hashColumn = Integer.parseInt(args[2]);

    ArrayList<PrintWriter> toDeliver = new ArrayList<PrintWriter>();
    for (int i = 0; i < numMachines; i++)
      toDeliver.add(new PrintWriter(new FileWriter(fname + "-intermediate-" + (i+1))));
    BufferedReader in = new BufferedReader(new FileReader(fname));
    String inputLine;

    String[] vals;
    int machine;

    while ((inputLine = in.readLine())!=null) {
      vals = inputLine.split("\t");
      machine = Math.abs(vals[hashColumn].trim().hashCode())%numMachines;
      toDeliver.get(machine).println(inputLine);
    }
    in.close();

    for (int i = 0; i < numMachines; i++) {
      toDeliver.get(i).close();
    }
  }
}
